﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
using System.Linq;
using System.Xml.Linq;
using System.Data.OleDb;

namespace iShoppingDAO
{
    public class LoaiSanPhamDAO
    {
        public static DataTable LayDSLoaiSanPham()
        {
            DataTable dt = null;
            try
            {
                string sql = "SELECT * FROM LoaiSanPham";
                dt = DataProvider.ExecuteQuery(sql);
            }
            catch (Exception ex)
            {

            }
            return dt;
        }

        public static string LayMaTheoTen(string ten)
        {
            string ma = "";
            try
            {
                string sql = "SELECT MaLoaiSanPham FROM LoaiSanPham where TenLoaiSanPham LIKE N'" + ten + "'";
                ma = (string)DataProvider.ExecuteScalar(sql);
            }
            catch (Exception ex)
            {

            }
            return ma;
        }
      
        public static bool UpdateLoaiSanPham(iShoppingDTO.LoaiSanPhamDTO loaiSanPhamDto)
        {
            bool res = false;
            try
            {
                List<SqlParameter> sqlParameters = new List<SqlParameter>();
                string sql = "UPDATE LoaiSanPham SET TenLoaiSanPham = @TenLoaiSanPham WHERE MaLoaiSanPham = @MaLoaiSanPham";
                SqlParameter[] parameter = new SqlParameter[2];

                parameter[0] = new SqlParameter("@TenLoaiSanPham", SqlDbType.NVarChar);
                parameter[0].Value = loaiSanPhamDto.TenLoaiSanPham;
                parameter[1] = new SqlParameter("@MaLoaiSanPham", SqlDbType.VarChar);
                parameter[1].Value = loaiSanPhamDto.MaLoaiSanPham;
  

                foreach (SqlParameter parameters in parameter)
                {
                    sqlParameters.Add(parameters);
                }
                res = DataProvider.ExecuteNoneQuery(sql, sqlParameters);

            }
            catch (Exception ex)
            {
                res = false;
            }
            return res;
        }

        public static bool DeleteLoaiSanPham(string maLoai)
        {
            bool res = false;
            try
            {
                List<SqlParameter> sqlParameters = new List<SqlParameter>();
                string sql = "DELETE FROM LoaiSanPham WHERE MaLoaiSanPham = @MaLoaiSanPham";
                SqlParameter[] parameter = new SqlParameter[1];
                parameter[0] = new SqlParameter("@MaLoaiSanPham", SqlDbType.VarChar);
                parameter[0].Value = maLoai;
                sqlParameters.Add(parameter[0]);

                res = DataProvider.ExecuteNoneQuery(sql, sqlParameters);

            }
            catch (Exception ex)
            {
                res = false;
            }
            return res;
        }

        public static bool InsertLoaiSanPham(iShoppingDTO.LoaiSanPhamDTO loaiSanPhamDto)
        {
            bool res = false;
            try
            {
                string sql = "INSERT INTO LoaiSanPham(MaLoaiSanPham,TenLoaiSanPham) VALUES(@MaLoaiSanPham,@TenLoaiSanPham)";
                List<SqlParameter> parameters = new List<SqlParameter>();

                SqlParameter[] par = new SqlParameter[2];
                par[0] = new SqlParameter("@MaLoaiSanPham", SqlDbType.NVarChar);
                par[0].Value = loaiSanPhamDto.MaLoaiSanPham;
                parameters.Add(par[0]);
                par[1] = new SqlParameter("@TenLoaiSanPham", SqlDbType.NVarChar);
                par[1].Value = loaiSanPhamDto.TenLoaiSanPham;
                parameters.Add(par[1]);

                res = DataProvider.ExecuteNoneQuery(sql, parameters);

            }
            catch (Exception)
            {
                res = false;
            }
            return res;
        }
    }
}
